package com.xbfinal.reggie.Filter;


import com.alibaba.fastjson.JSON;
import com.xbfinal.reggie.common.BaseContext;
import com.xbfinal.reggie.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 检查是否登陆
 * urlPatterns拦截的路劲   /* 表示所有请求
 * Filter是javax下的
 * 别忘记在启动类上加 @ServletComponentScan注解
 */
@Slf4j
@WebFilter(filterName = "LoginCheckFilter",urlPatterns = "/*")
public class LoginCheckFilter implements Filter {
    //路径匹配器
    public static final AntPathMatcher PATH_MATCHER
            =new AntPathMatcher();

    @Override
    public void doFilter(ServletRequest servletRequest,
                         ServletResponse servletResponse,
                         FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request=(HttpServletRequest)servletRequest;
        HttpServletResponse  response=(HttpServletResponse)servletResponse;

        //1.获取本次请求的url
        final String requestURI = request.getRequestURI();

        log.info("拦截到请求："+requestURI);
        //2.本次是否需要处理(检查登陆状态、不需要过滤的页面 如登陆页面)
        String[] uls = new String[]{
          //不需要处理的请求,静态资源等
             "/employee/login" ,
             "/employee/logout",
             "/backend/**"   ,
             "/front/**",
                "/",
                "/favicon.ico",
                "/user/sendMsg",
                "/user/login"

        };
        final boolean check = check(uls, requestURI);

        //3.如果不需要处理就放行
        if(check){
            //放行
            filterChain.doFilter(request,response);
            //结束语句
            return;
        }

        //4.判断登陆状态 如果登陆则放行
        final Object employee = request.getSession().getAttribute("employee");
        if(employee!= null){
            //插入基于ThreadLocal封装的工具类
            final Long empId
                    = (Long)request.getSession().getAttribute("employee");
            BaseContext.setCurrentId(empId);
            //放行
            filterChain.doFilter(request,response);
            return;
        }
        //4.判断移动端用户是否登陆
        final Object user = request.getSession().getAttribute("user");
        if(user!= null){
            //插入基于ThreadLocal封装的工具类
            final Long userId
                    = (Long)request.getSession().getAttribute("user");
            BaseContext.setCurrentId(userId);//绑定当前id
            //放行
            filterChain.doFilter(request,response);
            return;
        }

        //返回登陆页面,通过输出流的方式向客户端相应数据
        //返回这个 NOTLOGIN 交给前端跳转
        response.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
        log.info("执行流操作");


        return;

    }

    public boolean check(String[] urls,String requestUrl){
        for (String url : urls) {
            final boolean match = PATH_MATCHER.match(url, requestUrl);
            //final boolean match = url.equals(requestUrl);
            if(match){
                return true;
            }
        }
        return false;
    }
}
